<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            let arr = [1, 2, 3, 4];
            // console.log(arr.splice(-2, 2));
            // console.log(arr);
            // console.log(arr.splice()); // []
            // console.log(arr); //  [1, 2, 3, 4]
            // console.log(arr.splice(1)); // [2,3,4]
            // console.log(arr); // [1]
            // let arr1 = [1, 2, 3, 4];
            // console.log(arr1.splice(0, 0)); // [1,2]
            // console.log(arr1); // [3,4]

            // let arr2 = [1, 2, 3, 4];
            // console.log(arr2.splice(0, 2, 5, 6)); // [1,2]
            // console.log(arr2); // [5,6,3,4]
            Array.prototype.mySplice = function (index, num, ...args) {
                let newArr = [];
                let returnArr = [];
                // 如果没有传入参数，直接返回空数组
                if (index == undefined || num == 0) return [];
                if (index < 0) index = index + this.length;
                // 如果没有传入截取数组的长度，自己手动计算
                if (!num) {
                    num = this.length - index + 1;
                } else if (num > 0) {
                    num = num + index;
                }
                for (let i = 0; i < index; i++) {
                    newArr.push(this[i]);
                }
                for (; index < num; index++) {
                    returnArr.push(this[index]);
                }
                // 如果传入其他参数，就传入的参数添加进去
                if (args.length > 0) {
                    newArr.push(...args);
                }
                for (let i = num; i < this.length; i++) {
                    newArr.push(this[i]);
                }
                for (let i = 0; i < newArr.length; i++) {
                    this[i] = newArr[i];
                }
                this.length = newArr.length;
                return returnArr;
            };
            console.log(arr.mySplice());
            console.log(arr.mySplice(1)); // [2,3,4]
            console.log(arr); // [1]
            let arr1 = [1, 2, 3, 4];
            console.log(arr1.mySplice(0, 0)); // [1,2]
            console.log(arr1); // [3,4]

            let arr2 = [1, 2, 3, 4];
            console.log(arr2.mySplice(0, 2, 5, 6)); // [1,2]
            console.log(arr2); // [5,6,3,4]
        </script>
    </body>
</html>
